﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace KBEngine.Setting.Internal
{
    [Serializable]
    public class DBmgr
    {
        /// <summary>
        /// 脚本入口模块， 相当于main函数
        /// </summary>
        public string entryScriptFile = "kbemain";

        /// <summary>
        /// debug模式下可输出读写操作信息
        /// </summary>
        public bool debug = false;

        /// <summary>
        /// 是否共享数据库
        /// </summary>
        public bool shareDB = false;

        /// <summary>
        /// 是否检查defs-MD5
        /// </summary>
        public bool allowEmptyDigest = false;

        /// <summary>
        /// 指定接口地址，可配置网卡名、MAC、IP
        /// </summary>
        public string internalInterface = string.Empty;
        [XmlElement("InterfacesServiceAddr")]
        public InterfacesServiceAddr interfacesServiceAddr = new InterfacesServiceAddr();

        /// <summary>
        /// 数据库接口
        /// </summary>
        public DatabaseInterfaces databaseInterfaces = new DatabaseInterfaces();

        /// <summary>
        /// 账号系统
        /// </summary>
        public AccountSystem account_system = new AccountSystem();
        public TelnetService telnet_service = new TelnetService();

        /// <summary>
        /// entityID分配器，进入溢出范围则请求获取新的ID资源 
        /// </summary>
        public Ids ids = new Ids();

        /// <summary>
        /// listen监听队列最大值
        /// </summary>
        [XmlElement("SOMAXCONN")]
        public uint somaxconn = 511;

        [Serializable]
        public class Ids
        {
            public int increasing_range = 2000;
        }

        [Serializable]
        public class InterfacesServiceAddr
        {
            /// <summary>
            /// 如果enable为false则相关数据不转发到Interfaces进程 
            /// 注意：如果为true，dbmgr认为数据将由interfaces交给第三方平台处理
            /// </summary>
            public bool enable = true;

            /// <summary>
            /// 如果为true，则自动将配置中的Interfaces地址加入到地址池，以免改动Interfaces端口需要修改多处
            /// </summary>
            public bool addDefaultAddress = true;

            /// <summary>
            /// Interfaces进程的地址（参考interfaces中地址配置）
            /// </summary>
            [XmlElement]
            public List<Address> item = new List<Address>();
        }

        [Serializable]
        public class Address
        {
            public string host = "localhost1";
            public int port = 0;

            public override string ToString()
            {
                return $"{host}:{port}";
            }
        }

        [Serializable]
        public class DatabaseInterfaces
        {
            [XmlElement("default")]
            public Default defaultt = new Default();

            [Serializable]
            public class Default
            {
                /// <summary>
                /// 如果为true，则为纯净的数据库，引擎不创建实体表 
                /// </summary>
                public bool pure = false;

                /// <summary>
                /// 数据库类型 （mysql、redis）
                /// </summary>
                public string type = "mysql";

                /// <summary>
                /// 数据库地址 
                /// </summary>
                public string host = "localhost";
                public uint port = 0;

                /// <summary>
                /// 数据库账号验证
                /// </summary>
                public Auth auth = new Auth();

                /// <summary>
                /// 数据库名称
                /// </summary>
                public string databaseName = "kbe";

                /// <summary>
                /// 数据库允许的连接数
                /// </summary>
                public ushort numConnections = 5;

                /// <summary>
                /// 字符编码类型
                /// </summary>
                public UnicodeString unicodeString = new UnicodeString();

                [Serializable]
                public class Auth
                {
                    public string username = "kbe";
                    public string password = "pwd123456";
                    public bool encrypt = true;
                }
                [Serializable]
                public class UnicodeString
                {
                    public string characterSet = "utf8mb4";
                    public string collation = "utf8mb4_bin";
                }
            }
        }

        [Serializable]
        public class AccountSystem
        {
            /// <summary>
            /// 账号Entity的名称
            /// </summary>
            public string accountEntityScriptType = "Account";

            /// <summary>
            /// 新账号默认标记(可组合，填写时按十进制格式) 
            ///	    常规标记(normal flag)   = 0x00000000
            ///		锁定标记(lock flag)     = 0x000000001
            ///		未激活标记(normal flag)  = 0x000000002
            /// </summary>
            public uint accountDefaultFlags = 0;

            /// <summary>
            /// 新账号默认过期时间(秒, 引擎会加上当前时间)
            /// </summary>
            public ulong accountDefaultDeadline = 0;

  
            public AccountResetPassword account_resetPassword = new AccountResetPassword();
            public AccountRegistration account_registration = new AccountRegistration();

            [Serializable]
            public class AccountResetPassword
            {
                /// <summary>
                /// 是否开放重设密码
                /// </summary>
                public bool enable = false;
            }
            [Serializable]
            public class AccountRegistration
            {
                /// <summary>
                /// 是否开放注册
                /// </summary>
                public bool enable = false;

                /// <summary>
                /// 登录合法时游戏数据库找不到游戏账号则自动创建
                /// </summary>
                public bool loginAutoCreate = false;
            }
        }
    }
}
